require("Physical")

Body = class("Body", Physical)

---

function Body:initialize(name, type)
	
	if not type 
	then
		type = "Body"
	end
		
	Physical.initialize(self, name, type)
	self:registerEntityType("Body")
		
	self.pockets = self:addSubContainer( Container:new(name .. "Pockets", "OnBody") )
end  

---

function Body:havePropInPockets(prop)

	return prop:isIn(self.pockets)
end

---

function Body:sensePossibleActions()

	Physical.sensePossibleActions(self)
	
	self:addPickupActionsToDirectory()
	self:addUnlockAndOpenActionsToDirectory()
	self:addTalkActionsToDirectory()
end

---

function Body:addPickupActionsToDirectory()

	self:getCollapsedProperty("CurrentContainer"):applyToCotainedEntities(self, "Prop", 
		function(self, prop)
		
			if prop:isPropertyCollasped("CurrentContainer") 
			then
				local sentence = Sentence:new()
				
				sentence:setSubject(self)
				sentence:setVerb("pickup")
				sentence:setDirectObject(prop)
				
				self:addPossibleActionToDirectory(sentence)
			end
		end
	)
end

---

function Body:addUnlockAndOpenActionsToDirectory()

	local doorsToNeighborhood = self:getCollapsedProperty("CurrentContainer").doorsToNeighborhood
	
	if 
		gField.entityTypes.Key and
		next(doorsToNeighborhood) 
	then
		for doorName, doorEntity in pairs(doorsToNeighborhood)
		do	
			local key = gField.entityTypes.Key[doorName]
			
			if 
				(not doorEntity:isEnabled()) and
				key and 
				key:isIn(self.pockets)  
			then
				local sentence = Sentence:new()
				
				sentence:setSubject(self)
				sentence:setVerb("unlockAndOpen")
				sentence:setDirectObject(doorEntity)

				self:addPossibleActionToDirectory(sentence)
			end
		end
	end
end

---

function Body:addTalkActionsToDirectory()
	
	-- talk verb is not implemented because this verb will be replaced by the mind into more precise discource verbs
	
	self:getCollapsedProperty("CurrentContainer"):applyToCotainedEntities(self, "Body", 
		function(self, body)
		
			if
				(body ~= self) and 
				body:isPropertyCollasped("CurrentContainer") 
			then
				local sentence = Sentence:new()
				
				sentence:setSubject(self)
				sentence:setVerb("talk")
				sentence:setDirectObject(body)
				
				self:addPossibleActionToDirectory(sentence)
			end
		end
	)
end


--- Verbs

function Body:pickup(sentence)

	local prop = sentence:getDirectObject()
	
	prop:collapseProperty("CurrentContainer", self.pockets)
	
	gLogger:log(self.type .. " " .. self.name .. " pickup " .. prop.type .. " " .. prop.name)
end

---

function Body:unlockAndOpen(sentence)

	local door = sentence:getDirectObject()
	
	door:enable()
	
	gLogger:log(self.type .. " " .. self.name .. " unlock & open " .. door.type .. " " .. door.name)
end

---

function Body:tell(sentence)

	local otherMind = sentence:getIndirectObject()
	local sentence = sentence:getDirectObject()
	
	otherMind:onDiscoveredEvent(sentence)
	
	gLogger:log(self.type .. " " .. self.name .. " told " .. otherMind.name .. " " .. tostring(sentence))
end